import { Injectable } from '@angular/core';
import { GanttApi } from '../gantt-chart/gantt.api';
import { RequestClientService } from '../request-client.service';

import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { BaseService } from '../base.service';
import { GanttModel } from '../gantt-chart/gantt.model';
import { OLDTREEURL, version } from 'src/app/config';

declare var Neon: any;
@Injectable()
export class ModelService {
  public currModelPage = false;
  public testParam = 0;
  public floorInfo: any = [];
  public selector: any = Neon.getSelector();
  public selectMemberInfo: any;
  public selectFloorInfo: any;

  public attrTempModelView = false;
  public attrAddModelView = false;

  public noneSelect = true;
  public modelId: string;
  public modelFileId: string;

  public currentUUid: string[];
  public uuid: string[];


  public attrMessageView = true;
  public panels: any = [];

  public _currentTask: GanttModel;
  public get currentTask() {
    return this._currentTask;
  }
  public set currentTask(value) {
    console.log(value);
    this._currentTask = value;
  }
  public customAttrList: any[] = [];
  public leftPanelShow = true;
  public rightPanelShow = false;
  public showGanttPanel = false;
  public noAttrWarning = false;
  public constructor(
    private requestClientService: RequestClientService,
    private ganttApi: GanttApi,
    private message: NzMessageService,
    private confirmServ: NzModalService,
    private baseSrv: BaseService
  ) {
    this.panels = [{
      active: false,
      disabled: false,
      name: '设计信息',
      label: 'design',
      detail: []
    }, {
      active: false,
      disabled: false,
      name: '属性',
      label: 'custom',
      childPanel: [
        {
          active: false,
          name: '构件属性',
          label: 'view',
          detail: []
        },
        {
          active: false,
          name: '自定义属性',
          label: 'control',
          type: 1,
          detail: []
        }
      ]
    }];

  }
  public rightSwitch() {
    this.rightPanelShow = !this.rightPanelShow;
  }
  public async leftSwitch() {
    this.leftPanelShow = !this.leftPanelShow;
  }

  public initListen() {
    this.selector.on('OnPropertyChanged', (ev: any) => {
      this.selectMemberInfo = ev.target.property;
      let uuids = this.selector.get();
      this.uuid = uuids;
      console.log(this.uuid);
      this.getItemAttr(uuids);
    });
  }

  public async getItemAttr(uuid: string[]) {
    if (uuid && uuid.length > 0) {
      let param = {
        modelId: this.modelFileId,
        shapeInstance: uuid[0]
      };
      let res = await this.requestClientService.get(OLDTREEURL + `/api/v${version}/BIMModel/getbyid`, param);
      this.panels[0].detail = [];
      if (res.success) {
        let Properties = JSON.parse(res.item).PropertySets[0].Properties;
        console.log(Properties);
        Properties.forEach((element) => {
          this.panels[0].detail.push({
            name: element.Name,
            value: element.Value
          });
        });
        console.log(this.panels.detail)
        this.noAttrWarning = false;
      } else {
        this.noAttrWarning = true;
      }
    } else {
      this.message.warning('请选择一个构件');
    }
  }
}
